﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KZ.Lottery.Entities;
using System.Data;
using System.Data.SqlClient;
using KZ.Lottery.Resources;
using KZ.Utilities;
using KZ.Exception;

namespace KZ.Lottery.DAL
{
    public class GroupGroupDAL
    {
        private static GroupGroup CreateGroupGroupFromReader(SqlDataReader reader)
        {
            GroupGroup groupGroup = new GroupGroup
            {
                GroupID = Utility.StringParse(reader[GroupGroupColumns.GroupID.ToString()]),
                GroupCode = Utility.StringParse(reader[GroupGroupColumns.GroupCode.ToString()]),
            };

            return groupGroup;
        }

        public static DataTable GetGroupGroupByCode(string groupCode)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("GroupCode", SqlDbType.VarChar).Value = groupCode;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.GroupGroupGetByGroupCode));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetGroupGroupByCode", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_GROUPGROUP_GET);
            }
        }

        public static List<GroupGroup> GetGroupGroupByGroupID(string groupID)
        {
            try
            {
                List<GroupGroup> ggList = new List<GroupGroup>();

                DataAccessHelper.SqlCmd.Parameters.Add("GroupID", SqlDbType.VarChar).Value = groupID;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.GroupGroupGetByGroupID))
                {
                    while (reader.Read())
                    {
                        ggList.Add(CreateGroupGroupFromReader(reader));
                    }
                }

                return ggList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetGroupGroupByGroupID", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_GROUPGROUP_GET);
            }
        }

        public bool InsertGroupGroup(DataTable dt)
        {
            try
            {
                DataAccessHelper.BeginTransaction(IsolationLevel.Serializable);

                string groupCode = Guid.NewGuid().ToString();
                foreach (DataRow row in dt.Rows)
                {
                    GroupGroup gg = new GroupGroup();
                    gg.GroupID = Utility.StringParse(row["GroupID"]);
                    gg.GroupCode = groupCode;

                    InsertGroupGroup(gg);
                }

                DataAccessHelper.CommitTransaction();
                return true;
            }
            catch (System.Exception ex)
            {
                Log.LogEx("InsertGroupGroup", ex);
                DataAccessHelper.RollbackTransaction();
                throw;
            }
        }

        public bool InsertGroupGroup(GroupGroup gg)
        {
            try
            {

                DataAccessHelper.SqlCmd.Parameters.Add("GroupID", SqlDbType.VarChar).Value = gg.GroupID;
                DataAccessHelper.SqlCmd.Parameters.Add("GroupCode", SqlDbType.VarChar).Value = gg.GroupCode;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.GroupGroupInsert) > 0)
                {
                    return true;
                }

                throw new DataAccessException(KZ_Message.BLM_ERR_RESULT_CHANNEL_EXIST);
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertGroupGroup", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RESULT_INSERT);
            }
        }
    }
}
